package icepuppet.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//游戏游玩界面
public class GameJFrame extends JFrame implements KeyListener, ActionListener {

    //记录难度
    String option = "easy";
    int amount = 3;
    int sum = 9;
    int size = 100;

    //图片初始化
    int[][] data = new int[5][5];
    //0的索引
    int x = 0;
    int y = 0;

    //胜利数组
    int[][] winEasy = new int[][]{
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 0},
    };
    int[][] winOrd = new int[][]{
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    int[][] winDiff = new int[][]{
            {1, 2, 3, 4, 5},
            {6, 7, 8, 9, 10},
            {11, 12, 13, 14, 15},
            {16, 17, 18, 19, 20},
            {21, 22, 23, 24, 0},
    };

    //记录步数
    int count = 0;


    //绑定动作监听的条目放至成员
    JMenuItem easyItem = new JMenuItem("简单");
    JMenuItem ordinaryItem = new JMenuItem("一般");
    JMenuItem difficultItem = new JMenuItem("困难");

    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem gameExitItem = new JMenuItem("关闭游戏");
    JMenuItem description = new JMenuItem("玩法说明");

    Random random = new Random();
    int num = random.nextInt(2)+1;

    //构造方法，初始化
    public GameJFrame() {
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //初始化数据，打乱图片
        initData();

        //初始化图片
        initImage();

        //让界面显示,写在最后
        this.setVisible(true);
    }

    //打乱图片（数组）
    private void initData() {
        int[] arr = new int[sum];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }

        Random random = new Random();
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            index = random.nextInt(arr.length);
            int tmp = arr[i];
            arr[i] = arr[index];
            arr[index] = tmp;
        }

        for (int i = 0; i < arr.length; i++) {
            //获得0的索引
            if (arr[i] == 0) {
                x = i / amount;
                y = i % amount;
            }
            data[i / amount][i % amount] = arr[i];
        }
    }

    //图片初始化
    private void initImage() {
        //清除图片
        this.getContentPane().removeAll();
        //细节
        //先加载的图片在上方
        //判断是否胜利
        if (victory(option)) {
            //创建ImageIcon的对象
            ImageIcon imageIcon = new ImageIcon("images/win.jpg");
            //创建一个JLabel容器对象
            JLabel jLabel = new JLabel(imageIcon);
            //设置图片位置
            jLabel.setBounds(150, 150, 200, 200);
            //将容器放到界面中
            this.getContentPane().add(jLabel);
        }

        //将步数加入界面
        JLabel jLabelCount = new JLabel("步数: " + count);
        jLabelCount.setSize(100, 50);
        jLabelCount.setBounds(60, 40, 100, 50);
        this.getContentPane().add(jLabelCount);


        //每列
        for (int i = 0; i < amount; i++) {
            //每行
            for (int j = 0; j < amount; j++) {
                //创建ImageIcon的对象
                ImageIcon imageIcon = new ImageIcon("images\\" + option + "\\" + num + "_" + data[i][j] + ".gif");
                //创建一个JLabel容器对象
                JLabel jLabel = new JLabel(imageIcon);
                //设置图片位置
                jLabel.setBounds(size * j + 100, size * i + 100, size, size);
                //设置图片边框
                //0：凸起
                //1：凹入
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                //将容器放到界面中
                this.getContentPane().add(jLabel);
            }
        }

        //加载背景图片
        ImageIcon background = new ImageIcon("images/background.jpg");
        JLabel jLabel = new JLabel(background);
        jLabel.setBounds(0, 0, 500, 500);
        this.getContentPane().add(jLabel);

        //更新图片
        this.getContentPane().repaint();
    }

    //菜单初始化
    private void initJMenuBar() {
        //创建整个菜单
        JMenuBar jMenuBar = new JMenuBar();

        //创建菜单上的选项
        JMenu function = new JMenu("功能");
        JMenu about = new JMenu("关于游戏");

        //创建选项下包含的条目
        JMenu optionItem = new JMenu("选择难度");

        //将条目绑定动作监听
        easyItem.addActionListener(this);
        ordinaryItem.addActionListener(this);
        difficultItem.addActionListener(this);

        replayItem.addActionListener(this);
        gameExitItem.addActionListener(this);

        description.addActionListener(this);

        //将条目添加到选项
        optionItem.add(easyItem);
        optionItem.add(ordinaryItem);
        optionItem.add(difficultItem);

        function.add(optionItem);
        function.add(replayItem);
        function.add(gameExitItem);

        about.add(description);

        //将选项添加到菜单里
        jMenuBar.add(function);
        jMenuBar.add(about);

        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);

    }

    //界面初始化
    public void initJFrame() {
        //设置长宽
        this.setSize(500, 500);
        //设置标题
        this.setTitle("拼图游戏");
        //设置界面始终位于最上方
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中
        this.setLayout(null);
        //添加键盘监听
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    //查看原图
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("按下不松");
        int keyCode = e.getKeyCode();
        System.out.println(keyCode);
        if (victory(option)) {
            return;
        }
        //加载原图
        if (keyCode == 32) {
            //清除界面图片
            this.getContentPane().removeAll();
            //创建ImageIcon的对象
            ImageIcon imageIcon = new ImageIcon("images/" + num + "_all.jpg");
            //创建一个JLabel容器对象
            JLabel jLabel = new JLabel(imageIcon);
            //设置图片位置
            jLabel.setBounds(100, 100, 300, 300);
            //放入界面
            this.getContentPane().add(jLabel);
            //加载背景图片
            ImageIcon background = new ImageIcon("images/background.jpg");
            JLabel jLabel1 = new JLabel(background);
            jLabel1.setBounds(0, 0, 500, 500);
            this.getContentPane().add(jLabel1);
            //刷新界面
            this.getContentPane().repaint();
        }

    }

    //键盘监听
    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("松开按键");
        int keyCode = e.getKeyCode();
        System.out.println(keyCode);
        if (victory(option)) {
            return;
        }
        if (keyCode == 37 || keyCode == 65) {
            System.out.println("向左");
            //将0右的图片与它交换
            if (y < amount - 1) {
                data[x][y] = data[x][y + 1];
                data[x][y + 1] = 0;
                y++;
                count++;
            }
            //重新加载图片
            initImage();

        } else if (keyCode == 38 || keyCode == 87) {
            System.out.println("向上");
            //将0下的图片与它交换
            if (x < amount - 1) {
                data[x][y] = data[x + 1][y];
                data[x + 1][y] = 0;
                x++;
                count++;
            }
            //重新加载图片
            initImage();

        } else if (keyCode == 39 || keyCode == 68) {
            System.out.println("向右");
            //将0左的图片与它交换
            if (y > 0) {
                data[x][y] = data[x][y - 1];
                data[x][y - 1] = 0;
                y--;
                count++;
            }
            //重新加载图片
            initImage();
        } else if (keyCode == 40 || keyCode == 83) {
            System.out.println("向下");
            //将0上的图片与它交换
            if (x > 0) {
                data[x][y] = data[x - 1][y];
                data[x - 1][y] = 0;
                x--;
                count++;
            }
            //重新加载图片
            initImage();
        } else if (keyCode == 32) {
            //松开后，回归
            initImage();
        } else if (keyCode == 86) {
            int num = 1;
            for (int i = 0; i < amount; i++) {
                for (int j = 0; j < amount; j++) {
                    data[i][j] = num;
                    num++;
                    System.out.println(data[i][j]);
                }
            }
            data[amount - 1][amount - 1] = 0;
            initImage();
        }
    }

    //判断是否胜利
    public boolean victory(String option) {
        for (int i = 0; i < amount; i++) {
            for (int j = 0; j < amount; j++) {
                if (option.equals("easy")) {
                    if (data[i][j] != winEasy[i][j]) {
                        return false;
                    }
                } else if (option.equals("ordinary")) {
                    if (data[i][j] != winOrd[i][j]) {
                        return false;
                    }
                } else if (option.equals("difficult")) {
                    if (data[i][j] != winDiff[i][j]) {
                        return false;
                    }
                }

            }
        }
        return true;
    }

    //动作监听
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == easyItem) {
            System.out.println("easy");
            option = "easy";
            amount = 3;
            sum = 9;
            size = 100;
            num = random.nextInt(2)+1;
            initGame();
        } else if (source == ordinaryItem) {
            System.out.println("ordinary");
            option = "ordinary";
            amount = 4;
            sum = 16;
            size = 75;
            num = random.nextInt(2)+1;
            initGame();
        } else if (source == difficultItem) {
            System.out.println("difficult");
            option = "difficult";
            amount = 5;
            sum = 25;
            size = 60;
            num = random.nextInt(2)+1;
            initGame();
        } else if (source == replayItem) {
            System.out.println("replay");
            initGame();
        } else if (source == gameExitItem) {
            System.out.println("exit");
            System.exit(0);
        } else if (source == description) {
            System.out.println("description");
            //创建一个弹框对象
            JDialog jDialog = new JDialog();
            jDialog.setTitle("玩法说明");
            //创建一个容器
            JLabel jLabel = new JLabel(new ImageIcon("images/description.jpg"));
            //设置位置
            jLabel.setBounds(0,0,500,375);
            //将容器放入弹框
            jDialog.getContentPane().add(jLabel);
            //设置弹框大小
            jDialog.setSize(520,400);
            //设置弹框置顶
            jDialog.setAlwaysOnTop(true);
            //弹框居中
            jDialog.setLocationRelativeTo(null);
            //弹框不结束不能进行其他操作
            jDialog.setModal(true);
            //显示弹框
            jDialog.setVisible(true);
        }
    }

    public void initGame() {
        //步数清零
        count = 0;
        //重新打乱顺序
        initData();
        //重新加载图片
        initImage();
    }
}
